using System;
using System.Linq;
using UnityEngine;
using UnityEngine.UI;
using Voodoo.Sauce.Internal;
using Voodoo.Sauce.Internal.Analytics;
using Voodoo.Sauce.Internal.VoodooTune;
using Voodoo.Tune.Core;

namespace Voodoo.Sauce.Debugger
{
	public class VoodooTuneHomeDebugScreen : Screen
	{
		private const string TAG = "VoodooTuneHomeDebugScreen";

		[Header("Loading Screen")]
		[SerializeField]
		private GameObject loadingRoot;

		[SerializeField]
		private GameObject normalDisplay;

		[SerializeField]
		private GameObject errorDisplay;

		[SerializeField]
		private Text errorLabel;

		[SerializeField]
		private Button retryButton;

		[Header("Home")]
		[SerializeField]
		private GameObject homeRoot;

		[SerializeField]
		private GameObject headerRoot;

		[SerializeField]
		private Screen abTestScreen;

		[SerializeField]
		private Screen segmentsScreen;

		[SerializeField]
		private Screen sandboxScreen;

		private void Start()
		{
			Load();
		}

		private void OnEnable()
		{
			retryButton.onClick.AddListener(Load);
		}

		private void OnDisable()
		{
			retryButton.onClick.RemoveListener(Load);
		}

		private void Load()
		{
			loadingRoot.SetActive(value: true);
			normalDisplay.SetActive(value: true);
			errorDisplay.SetActive(value: false);
			headerRoot.SetActive(value: false);
			homeRoot.SetActive(value: false);
			DebugVTManager.LoadDebugConfigurations(OnSuccess, OnError);
		}

		private void OnSuccess()
		{
			OnInitDone();
			loadingRoot.SetActive(value: false);
			headerRoot.SetActive(value: true);
			homeRoot.SetActive(value: true);
		}

		private void OnError(string message)
		{
			normalDisplay.SetActive(value: false);
			errorDisplay.SetActive(value: true);
			errorLabel.text = message;
		}

		private void OnInitDone()
		{
			DebugVTManager.NewDebugDraftConfiguration();
			ClearDisplay();
			OpenFoldout("VoodooTune Debug");
			Button("A/B Tests", OnAbTestButtonClick).Interactable = DebugVTManager.GetDebugAbTests().Any();
			Button("Segments", OnSegmentsButtonClick).Interactable = DebugVTManager.GetDebugSegments().Any();
			Button("Sandbox", OnSandboxButtonClick).Interactable = DebugVTManager.GetDebugSandboxes().Any();
			CloseFoldout();
			CreateInitRequest();
		}

		private void CreateInitRequest()
		{
			VoodooTuneInitAnalyticsInfo infos = VoodooTuneManager.LastVoodooTuneInitInfo.infos;
			string value = VoodooTuneManager.LastVoodooTuneInitInfo.error ?? "Success";
			string value2 = infos.HttpResponseCode + " - " + GetResponseName(infos.HttpResponseCode);
			string value3 = (infos.HasCache ? "Yes" : "No");
			string value4 = ((double)infos.PayloadSize / 1024.0).ToString("0.##") + "Kb";
			long? deserializationTime = infos.DeserializationTime;
			string rawConfiguration = VoodooTuneManager.GetRawConfiguration();
			bool flag = VoodooTunePersistentData.SavedConfig == rawConfiguration;
			OpenFoldout("Init Request");
			Label("Request result", value);
			Label("Http Response", value2);
			Label("Configuration was applied", flag.ToString());
			Label("Duration", infos.DurationInMilliseconds + "ms");
			Label("Timeout used", VoodooTuneManager.CurrentTimeoutInMilliseconds + "ms");
			Label("Has Cache", value3);
			Label("Payload Size", value4);
			if (deserializationTime.HasValue)
			{
				long? num = deserializationTime;
				Label("Deserialization Time", num + "ms");
			}
			InputField("New Timeout", "Enter Timeout...", OnTimeoutInputEndEdit);
			CopyToClipboard("Copy URL", VoodooTunePersistentData.SavedURL);
			CloseFoldout();
		}

		private void OnAbTestButtonClick()
		{
			Debugger.Show(abTestScreen);
		}

		private void OnSegmentsButtonClick()
		{
			Debugger.Show(segmentsScreen);
		}

		private void OnSandboxButtonClick()
		{
			Debugger.Show(sandboxScreen);
		}

		private static void OnTimeoutInputEndEdit(string text)
		{
			if (string.IsNullOrEmpty(text))
			{
				return;
			}
			try
			{
				DebugVTLocalTimeout.SetLocalTimeout(int.Parse(text));
			}
			catch (FormatException)
			{
				VoodooLog.LogError(Module.VOODOO_TUNE, "VoodooTuneHomeDebugScreen", "Can't convert " + text + " to a valid timeout.");
			}
		}

		private static string GetResponseName(double httpCode)
		{
			if (httpCode <= 200.0)
			{
				if (httpCode == -1.0)
				{
					return "VoodooTune Timeout";
				}
				if (httpCode == 0.0)
				{
					return "Couldn't reach server (bad url?)";
				}
				if (httpCode == 200.0)
				{
					return "Success";
				}
			}
			else if (httpCode <= 403.0)
			{
				if (httpCode == 204.0)
				{
					return "No configuration for this app";
				}
				if (httpCode == 403.0)
				{
					return "Unauthorized";
				}
			}
			else
			{
				if (httpCode == 404.0)
				{
					return "Not found";
				}
				if (httpCode == 422.0)
				{
					return "Unprocessable Entity";
				}
			}
			return "";
		}
	}
}
